class Main {
  constructor() {
    let m = {
      //设置并初始化阳光总数
      allSun: 200,
      //全局定时器(生产阳光定时器)
      sunTimer: null,
      //定时生产阳光的差值
      sun_spacing: 30,
      //创建小车对象
      cars: [],
      cars_info: {
        x: 98,
        y: 80,
        position: [
          { row: 1, col: 0 },
          { row: 2, col: 0 },
          { row: 3, col: 0 },
          { row: 4, col: 0 },
          { row: 5, col: 0 },
        ]
      },
      //实例化卡片数组
      cards: [],
      cards_info: {
        x: 0,
        y: 0,
        position: [
          { name: 'sunflower', col: 1, sun_val: 50, timer_spacing: 7 * 1000 },
          { name: 'peashooter', col: 2, sun_val: 100, timer_spacing: 1 * 1000 },
          { name: 'cherrybomb', col: 3, sun_val: 150, timer_spacing: 25 * 1000 },
          { name: 'wallnut', col: 4, sun_val: 50, timer_spacing: 20 * 1000 },
          { name: 'chomper', col: 5, sun_val: 150, timer_spacing: 1 * 1000 },
        ]
      },
      //一共生成出总僵尸的个数
      zombieNum: 0,
      fps: 60,
      plants: [],
      plant_info: {
        //初始化x坐标
        x: 165,
        //初始化y坐标
        y: 80,
        position: [
          // { section: 'peashooter', row: 1, col: 1 },
          // { section: 'peashooter', row: 2, col: 1 },
          // { section: 'peashooter', row: 3, col: 1 },
          // { section: 'peashooter', row: 4, col: 1 }
        ]
      },
      zombiesTimer: null,
      zombies: [],
      zombie_info: {
        //初始化x坐标
        x: 960,
        //初始化y坐标
        y: 25,
        position: [
          // { row: Math.floor(Math.random() * 5) + 1, col: 10 },
          // { row: 1, col: 10 },
          // { row: 4, col: 10 },
          // { row: 5, col: 10 },
          // { row: 5, col: 10 },

        ]
      },
      zombiesnum: 0,
      game: null,
    }
    Object.assign(this, m)
  }

  setCards(cards_info) {
    let self = this
    for (let card of cards_info.position) {
      //初始化对象
      let info = {
        name: card.name,
        col: card.col,
        sun_val: card.sun_val,
        timer_spacing: card.timer_spacing,
        x: cards_info.x + 53 * (card.col - 1) + 71,
        y: 7
      }

      //将实例添加到卡片数组
      self.cards.push(Card.new(info))
    }
  }

  setCars(cars_info) {
    let self = this
    for (let car of cars_info.position) {
      let info = {
        x: cars_info.x,
        y: cars_info.y + 100 * (car.row - 1),
        col: car.col,
        row: car.row
      }
      if (info.row === 5) {
        info.x -= 10
      }
      self.cars.push(Car.new(info))
    }
  }

  setPlants(plant_info) {
    let self = this
    for (let plant of plant_info.position) {
      let info = {
        section: plant.section,
        x: plant_info.x + 90 * (plant.col - 1),
        y: plant_info.y + 100 * (plant.row - 1),
        col: plant.col,
        row: plant.row,
      }
      self.plants.push(Plant.new(info))
      // console.log(self.plants)
    }
  }

  setZombies(zombie_info) {
    let self = this;
    for (let zombie of zombie_info.position) {
      let info = {
        col: zombie.col,
        row: zombie.row,
        y: zombie_info.y + 90 * (zombie.row - 1),
        x: zombie_info.x
      };
      if (info.row === 1) {
        info.y -= 10
      } else if (info.row === 5) {
        info.y += 8
      }
      console.log(info.x, info.y)
      self.zombies.push(Zombie.new(info))
    }
  }

  setSunTimer() {
    let self = this
    self.sunTimer = setInterval(function () {
      let img = document.createElement('img')
      img.src = 'images/Sun.gif'
      let container = document.querySelector('.game-jframe')

      // 随机生成太阳图片的位置
      let randomX = Math.floor(Math.random() * (500 - 300 + 1)) + 300
      let randomY = 0

      img.style.top = `${randomY}px`
      img.style.left = `${randomX}px`
      img.className = 'main_sun'

      container.appendChild(img)
      window.main.allSun += 25

      // 监听动画结束事件
      img.addEventListener('animationend', function () {
        if (img.parentNode) {
          img.parentNode.removeChild(img)
        }
      })
    }, self.sun_spacing * 1000)
  }

  clearSunTimer() {
    let self = this
    if (self.sunTimer) {
      clearInterval(self.sunTimer)
      self.sunTimer = null
    }
  }

  clearZombies() {
    let self = this
    if (self.zombiesTimer) {
      clearInterval(self.zombiesTimer)
      self.zombiesTimer = null
    }
  }

  setTimerZombies() {
    let self = this;
    self.zombiesTimer = setInterval(function () {
      let row = Math.floor(Math.random() * 5) + 1
      let info = {
        col: 9,
        row: row,
        y: 25 + 90 * (row - 1),
        x: 900
      }
      if (info.row === 1) {
        info.y -= 10
      } else if (info.row === 5) {
        info.y += 14
      }
      // console.log(info.x, info.y)
      let zombie = Zombie.new(info)
      self.zombies.push(zombie)
      self.zombiesnum++
      self.zombieNum++
    }, 20000)

  }



  start() {
    let self = this
    self.setPlants(self.plant_info)
    self.setZombies(self.zombie_info)
    self.game = Game.new()

  }


  reset() {
    let self = this
    for (let i = self.plants.length - 1; i >= 0; i--) {
      const plant = self.plants[i];
      plant.life = -1;
      if (plant.life === -1) {
        self.plants.splice(i, 1); // 从数组中移除植物
      }
    }

    for (let i = self.zombies.length - 1; i >= 0; i--) {
      const zombie = self.zombies[i];
      zombie.life = -1;
      if (zombie.life === -1) {
        self.zombies.splice(i, 1); // 从数组中移除僵尸
      }
    }



    // 清除所有计时器
    self.clearSunTimer()
    self.clearZombies()
    const sun = document.querySelectorAll('.sun')
    const main_sun = document.querySelectorAll('.main_sun')
    sun.forEach(sun1 => {
      if (sun1.parentNode) {
        console.log('Parent node exists:', sun1.parentNode)
        sun1.parentNode.removeChild(sun1)
      } else {
        console.warn('No parent node for element:', sun1)
      }
    })
    main_sun.forEach(main_sun1 => {
      if (main_sun1.parentNode) {
        console.log('Parent node exists:', main_sun1.parentNode);
        main_sun1.parentNode.removeChild(main_sun1);
      } else {
        console.warn('No parent node for element:', main_sun1);
      }
    })


    // 清空所有游戏对象
    self.cards = []
    for (let card of self.cards) {
      card.isspacing = false
      card.timer_num = 0
    }
    self.cars = []

    // 重新初始化游戏状态
    self.allSun = 2200
    self.zombieNum = 0
    self.zombiesnum = 0

    audio2.pause()
    audio1.play()
    audio1.loop = true
    audio1.auotplay = true;


    self.game.state = 1
  }
}

window.main = new Main()
window.main.start()